package com.servlet;

import com.dao.Impl.ProvinceDaoImpl;
import com.dao.ProvinceDao;
import com.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/provinceServlet/*")
public class ProvinceServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String url=request.getRequestURI();
        String method=url.substring(url.lastIndexOf("/")+1);
        String target=null;
        switch (method){
            case "findProvince":
                target=findProvince(request,response);
                break;

        }
        if (target!=null){
            request.getRequestDispatcher(target).forward(request,response);
        }
    }


    /**
     * 查找所有省份
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String findProvince(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProvinceDao dao=new ProvinceDaoImpl();
        List<Map> allProvince = dao.findAllProvince();
        MyUtil.jsonWriter(response,allProvince);

        return  null;
    }
}
